package com.cn.gmall.common.service;


import ch.qos.logback.core.CoreConstants;
import com.alibaba.fastjson.JSONObject;
import com.cn.gmall.common.mqconst.MqConst;
import com.cn.gmall.common.pojo.GmallCorrelationData;

import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.lang.management.GarbageCollectorMXBean;
import java.nio.charset.CoderResult;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Service
public class RabbitService {

    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Autowired
    private RedisTemplate redisTemplate;

    /***
     *发送延时消息到队列
     * @param exchange
     * @param routingKey
     * @param msg
     * @param delayTime
     * @return
     */
    public boolean sendDelayMsg(String exchange, String routingKey, Object msg, int delayTime) {
//    消息参数对象
        GmallCorrelationData gmallCorrelationData = new GmallCorrelationData();

//id
        String correlationId = UUID.randomUUID().toString().replaceAll("-", "");
        gmallCorrelationData.setId(correlationId);

        gmallCorrelationData.setExchange(exchange);
        gmallCorrelationData.setRoutingKey(routingKey);
        gmallCorrelationData.setMessage(msg);
        gmallCorrelationData.setDelay(true);
        gmallCorrelationData.setDelayTime(delayTime);
//        将数据存到缓存(30分钟过期)
        redisTemplate.opsForValue().set(
                MqConst.MQ_KEY_PREFIX + correlationId,
                JSONObject.toJSONString(gmallCorrelationData),
                30,
                TimeUnit.MINUTES
        );

//发消息
        /*** MessagePostProcessor 可以在消息接收前后 对消息进行处理(增加修改 删除 压缩)
         */
        rabbitTemplate.convertAndSend(exchange, routingKey, msg, message -> {
            message.getMessageProperties().setDelay(delayTime*1000);  //将单位ms->s
            return message;
        },gmallCorrelationData);

        return true;
    }

    /***
     * 发送普通消息
     * @param exchange
     * @param routingKey
     * @param message
     * @return
     */
    public boolean sendMessage(String exchange, String routingKey, Object message) {
//        CorrelationData correlationData1 = new CorrelationData();
        //穿件消息数据对象
        GmallCorrelationData correlationData = new GmallCorrelationData();
//设置id
        String correlationId = UUID.randomUUID().toString().replaceAll("-", "");
        correlationData.setId(correlationId);
//设置交换机
        correlationData.setExchange(exchange);
//       设置路由
        correlationData.setRoutingKey(routingKey);
//        设置消息
        correlationData.setMessage(message);

        redisTemplate.opsForValue().set(
                MqConst.MQ_KEY_PREFIX + correlationId,
                JSONObject.toJSONString(correlationData),
                30,
                TimeUnit.MINUTES
        );

        rabbitTemplate.convertAndSend(exchange, routingKey, message, correlationData);
        return true;
    }

    /***
     * 发送普通的消息到队列
     * @param exchange 交换机
     * @param routingKey 路由键
     * @param message  消息
     * @return
     */
   /* public boolean sendMessage(String exchange, String routingKey, Object message) {
        rabbitTemplate.convertAndSend(exchange, routingKey , message);
        return  true;
    }*/

}
